package com.captain.leetcode.链表;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/3/19 15:55
 */
public class 两两交换链表中的节点24 {
    public static void main(String[] args) {
        new 两两交换链表中的节点24().swapPairs(ListNode.getInstance());
    }

    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(0, head);
        ListNode pre = dummy;
        //退出条件
        while (pre.next != null && pre.next.next != null) {
            //12 3456
            //记录head的下一次指针
            ListNode temp = head.next.next;
            //进行两两相交
            pre.next = head.next;
            pre.next.next = head;
            //相交完之后的next = temp
            // 134
            head.next = temp;
            pre = head;
            head = head.next;
        }
        return dummy.next;
    }
}
